/**
 * 
 */
package com.cmcc.awareness;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.xml.bind.DatatypeConverter;

/**
 * @author Amish
 * @date 2018年12月7日 下午7:14:48
 * 
 * 任意位置操作符表达式：x
 * 
 * eg1:
 * {x|9|0x3d225468756e646572}
 * 
 * eg2:
 * {x|8|PPStream}
 */
public class ProtoDatProtocolFeatureCodeElement_x implements ProtoDatProtocolFeatureCodeElement {
	private static Logger logger = LogManager.getLogger(ProtoDatProtocolFeatureCodeElement_x.class);

    String initialStr;
	
	String valueLen;
	String value;//如果有0x，并不去掉
	
	public String appName;
	public String protocolType;
	public ProtoDatProtocolFeatureCodeElement_x(String appName,String protocolType){
		this.appName = appName;
		this.protocolType = protocolType;
	}
	
	@Override
	public String getType() {
		return "x";
	}

	
	@Override
	public boolean distinguish(String protocolType,jpcap.packet.Packet packet) {
		try{
            byte[] data = packet.data;
			
			if(data == null || data.length == 0 )
				return false;
			
			int valueLenInt = Integer.valueOf(this.valueLen);
			int valueInt = Integer.valueOf(this.value.length());//Value长度
			
		    if(this.value.startsWith("0x")){
		    	if(valueLenInt != (valueInt/2-1) || data.length < (valueInt/2-1)){
		    		return false;
		    	}
		    	
		    	String dataHexStr = DatatypeConverter.printHexBinary(data);
		        if(dataHexStr.contains(this.value.substring(2))){
		        	return true;
		        }
		    	
		    }else{
		    	if(valueLenInt != valueInt || data.length < valueLenInt ){
		    		return false;
		    	}
		    	
		    	String dataStr = new String(data,"UTF-8");
		    	if(dataStr.contains(this.value)){
		    		return true;
		    	}
		    }

		    return false;
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Distinguish Exception -- <"+this.appName+">"+"<"+this.protocolType+"> : "+this.toString());
			return false;
		}
	}

	@Override
	public boolean distinguish(String protocolType,org.pcap4j.packet.Packet packet) {
		try{
            byte[] data = packet.getRawData();
			
			if(data == null || data.length == 0 )
				return false;
			
			int valueLenInt = Integer.valueOf(this.valueLen);
			int valueInt = Integer.valueOf(this.value.length());//Value长度
			
		    if(this.value.startsWith("0x")){
		    	if(valueLenInt != (valueInt/2-1) || data.length < (valueInt/2-1)){
		    		return false;
		    	}
		    	
		    	String dataHexStr = DatatypeConverter.printHexBinary(data);
		        if(dataHexStr.contains(this.value.substring(2))){
		        	return true;
		        }
		    	
		    }else{
		    	if(valueLenInt != valueInt || data.length < valueLenInt ){
		    		return false;
		    	}
		    	
		    	String dataStr = new String(data,"UTF-8");
		    	if(dataStr.contains(this.value)){
		    		return true;
		    	}
		    }

		    return false;
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Distinguish Exception -- <"+this.appName+">"+"<"+this.protocolType+"> : "+this.toString());
			return false;
		}
	}
	@Override
	public void parseSelf(String element) {
		try{			
			this.initialStr = element;
			String strs[] = element.split("\\|");
			this.valueLen = strs[1];
			this.value = strs[2];

		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public String toString(){
		return "{"+initialStr+"}";
	}

}
